package com.personal.modules.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.personal.common.bean.PageUtils;
import com.personal.common.bean.ResultMsg;
import com.personal.common.util.Constants;
import com.personal.common.util.Query;
import com.personal.modules.sys.entity.TSysDeptEntity;
import com.personal.modules.sys.service.TSysDeptService;

/**
 * 部门管理
 * 
 * @author liuyuzhu
 * @email liuyuzhu.1314@gmail.com
 * @date 2017-10-09 16:34:04
 */
@RestController
@RequestMapping("/tsysdept")
public class TSysDeptController extends AbstractController{
	@Autowired
	private TSysDeptService tSysDeptService;
	
	@RequestMapping("/deptList")
	public ModelAndView deptList(HttpServletRequest request){
		return new ModelAndView("modules/sys/dept/dept");
	}
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	//@RequiresPermissions("tsysdept:list")
	public ResultMsg list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<TSysDeptEntity> tSysDeptList = tSysDeptService.queryList(query);
		int total = tSysDeptService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(tSysDeptList, total, query.getLimit(), query.getPage());
		
		return new ResultMsg(pageUtil);
	}
	
	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	//@RequiresPermissions("sys:dept:select")
	public ResultMsg select(){
		List<TSysDeptEntity> deptList = tSysDeptService.queryList(new HashMap<String, Object>());

		//添加一级部门
		if(getUserId() == Constants.SUPER_ADMIN){
			TSysDeptEntity root = new TSysDeptEntity();
			root.setDeptId(0L);
			root.setName("一级部门");
			root.setParentId(-1L);
			root.setOpen(true);
			deptList.add(root);
		}

		return new ResultMsg(deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@RequestMapping("/info")
	//@RequiresPermissions("sys:dept:list")
	public ResultMsg info(){
		long deptId = 0;
		if(getUserId() != Constants.SUPER_ADMIN){
			TSysDeptEntity dept = tSysDeptService.queryObject(getDeptId());
			deptId = dept.getParentId();
		}

		return new ResultMsg(deptId);
	}
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	@RequiresPermissions("tsysdept:info")
	public ResultMsg info(@PathVariable("deptId") Long deptId){
		TSysDeptEntity tSysDept = tSysDeptService.queryObject(deptId);
		
		return new ResultMsg(tSysDept);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("tsysdept:save")
	public ResultMsg save(@RequestBody TSysDeptEntity tSysDept){
		tSysDeptService.save(tSysDept);
		
		return new ResultMsg();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("tsysdept:update")
	public ResultMsg update(@RequestBody TSysDeptEntity tSysDept){
		tSysDeptService.update(tSysDept);
		
		return new ResultMsg();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("tsysdept:delete")
	public ResultMsg delete(@RequestBody Long[] deptIds){
		tSysDeptService.deleteBatch(deptIds);
		
		return new ResultMsg();
	}
	
}
